<?php
/**
*
* ClassName: Mobility_ip_delayed_model
*
* 延时 ip 模型...
*
* @author hejigui <hejigui@soften.cn>
*
*/
class Mobility_ip_delayed_model extends CI_Model {

    private $_write_db;
    function __construct()
    {
        parent::__construct();
        $this->_write_db = $this->load->database('master', TRUE);
    }


	/**
    * 记录 延时的ip
    *
    * @access public
    * @param array $ip_info 延时的ip信息 [Must]
    * @return void
    */
    public function log_delayed_ip($ip_info) {
    	if(empty($ip_info['source']) || empty($ip_info['ctime']) || empty($ip_info['source'])){
    		return false;
    	}

        $data['source'] = $ip_info['source'];
        $data['ip_ctime'] = $ip_info['ctime'];
        $data['ctime'] = time();

        $this->_write_db->insert('mobility_ip_delayed', $data);
    }

  /**
    * 查看adsl服务器 在指定时间段内 网络请求错误数
    *
    * @access public
    * @param array $time_rang_type 要查看的时间范围类别 today 当天  week 本周 month 本月 [Optional]
    * @return array $mixed
    */
    public function get_delayed_count($time_rang_type = 'today') {
		$sql = "SELECT count(*) as c ,source FROM  `mobility_ip_delayed`";

		switch ($time_rang_type) {
			case 'today':
				$sql .= " where  ctime > UNIX_TIMESTAMP( CURDATE( ) )";
				break;
			case 'week':
				$sql .= " where  ctime > unix_timestamp(DATE_ADD(CURDATE(), INTERVAL -7 DAY))";
				break;
			case 'month':
				$sql .= " where  ctime > unix_timestamp(DATE_ADD(CURDATE(), INTERVAL -30 DAY))";
				break;
			default:
				$sql .= " where  ctime > UNIX_TIMESTAMP( CURDATE( ) )";
				break;
		}

		$sql .= " GROUP BY  `source`  ORDER BY c DESC";

		return $this->db->query($sql)->result_array();
    }
}